import traceback
from bs4 import BeautifulSoup
from crawlers.utils import fetch_json_data, fetch_webpage
from crawlers.constants import REQUEST_HEADERS, ZHIHU_HOT_API

def get_articles_from_zhihu():
    """从知乎获取热榜文章列表
    
    Returns:
        文章列表，每个文章包含序号、标题、链接、热度值和简介
    """
    try:
        webpage = fetch_webpage(ZHIHU_HOT_API, headers=REQUEST_HEADERS)
        soup = BeautifulSoup(webpage, 'html.parser')

        hot_questions = []
        questions_table = soup.select_one(".jc-c table")
        questions = questions_table.select("tr")
        for index, item in enumerate(questions, 1):
            trs = item.find_all('td')
            if len(trs) < 4:
                continue

            title_element = trs[1].select_one('a')
            title = title_element.get_text(strip=True) if title_element else ''
            url = title_element['href'] if title_element else ''

            hot = int(float(trs[2].get_text(strip=True).replace('万热度', '').strip()) * 10000)

            hot_question_info = {
                'order': index,
                'title': title,
                'url': url,
                'hot': hot,
            }

            hot_questions.append(hot_question_info)
        
        return hot_questions
    except Exception as e:
        print("知乎热榜数据获取失败:", e)
        print("详细错误信息:")
        traceback.print_exc()
        raise